//
//  ViewController.m
//  测试
//
//  Created by admin on 2017/9/1.
//  Copyright © 2017年 DK. All rights reserved.
//

#import "ViewController.h"
#import "AnomalyShapeChangeView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@property (nonatomic, strong) UIImageView *bkImageView;
@property (nonatomic, strong) AnomalyShapeChangeView *anomalyView;
@property (nonatomic, assign) NSInteger nearest;// 滑动开始时距离起始点最近的点的下标
@property (nonatomic, assign) CGPoint initialCenter;// 初始位置的 center
@property (nonatomic, assign) CGRect initialFrame;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    imageView.image  = [UIImage imageNamed:@"120"];
    [self.view addSubview:imageView];
    
    self.bkImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0.f, kScreenWidth, kScreenHeight)];
    self.bkImageView.image = [UIImage imageNamed:@"110.jpg"];
    self.bkImageView.userInteractionEnabled = YES;
    [self.view addSubview:self.bkImageView];
    
    self.nearest = -1;
    NSMutableArray *pointArray = [NSMutableArray arrayWithCapacity:0];
    // 添加初始位置的点
    NSValue *point = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
    NSValue *point2 = [NSValue valueWithCGPoint:CGPointMake(50, 200)];
    NSValue *point3 = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
    NSValue *point4 = [NSValue valueWithCGPoint:CGPointMake(200, 50)];
    [pointArray addObject:point];
    [pointArray addObject:point2];
    [pointArray addObject:point3];
    [pointArray addObject:point4];
    self.anomalyView = [[AnomalyShapeChangeView alloc] initWithFrame:CGRectMake(0, 0.f, _bkImageView.frame.size.width, _bkImageView.frame.size.height)];
    [self.anomalyView addInitialPoints:pointArray];
    [self.view addSubview:self.anomalyView];
    
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
    singleTap.numberOfTapsRequired = 1;
    singleTap.numberOfTouchesRequired = 1;
    [self.anomalyView addGestureRecognizer:singleTap];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    pan.maximumNumberOfTouches = 1;
    [self.anomalyView addGestureRecognizer:pan];
}

#pragma mark 导航栏右侧按钮
- (void)rightBarButtonAction:(UIBarButtonItem *)rightItem {

    UIBezierPath *patn = [UIBezierPath bezierPath];
    NSArray *points = [self.anomalyView.points copy];
    
    if (points.count < 3) {
        return;
    }
    
    for (NSInteger i = 0 ; i < points.count; i ++) {
        UIView *view = [points objectAtIndex:i];
        if (i == 0) {
            [patn moveToPoint:view.center];
        }else {
            [patn addLineToPoint:view.center];
        }
    }
    [patn closePath];
    
    UIBezierPath *test = [UIBezierPath bezierPathWithRect:self.anomalyView.bounds];
    [test appendPath:patn];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = test.CGPath;
    layer.fillRule = kCAFillRuleEvenOdd;
    self.bkImageView.layer.mask = layer;
}
#pragma mark - 单击手势添加点
- (void)singleTapAction:(UITapGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:self.anomalyView];
    [self.anomalyView addPointAt:point];
    [self rightBarButtonAction:nil];
}

- (void)panGesture:(UIPanGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:self.anomalyView];
    CGPoint transPoint = [gesture translationInView:self.anomalyView];
    // 找出距离添加点最近的点 (下标)
    if (-1 == self.nearest) {
        CGFloat nearestDistance = 10000;
        for (int i = 0; i < self.anomalyView.points.count; i ++) {
            UIView *view = self.anomalyView.points[i];
            CGPoint toPoint = view.center;
            CGFloat distance = [self.anomalyView distanceFrom:point to:toPoint];
            if (distance < nearestDistance) {
                nearestDistance = distance;
                self.nearest = i;
            }
        }
        UIView *pointView = self.anomalyView.points[self.nearest];
        self.initialCenter = pointView.center;
        self.initialFrame = pointView.frame;
    }
    UIView *pointView = self.anomalyView.points[self.nearest];
    CGRect frame = CGRectMake(self.initialFrame.origin.x + transPoint.x, self.initialFrame.origin.y + transPoint.y, self.initialFrame.size.width, self.initialFrame.size.height);
    CGFloat originx = frame.origin.x;
    CGFloat originy = frame.origin.y;
    if (0 > originx) {
        originx = 0;
    }
    if (self.anomalyView.frame.size.width < originx) {
        originx = self.anomalyView.frame.size.width - frame.size.width;
    }
    if (0 > originy) {
        originy = 0;
    }
    if (self.anomalyView.frame.size.height < originy) {
        originy = self.anomalyView.frame.size.height - frame.size.height;
    }
    frame = CGRectMake(originx, originy, frame.size.width, frame.size.height);
    pointView.frame = frame;
//    CGPoint newCenter = CGPointMake(self.initialCenter.x + transPoint.x, self.initialCenter.y + transPoint.y);
//    pointView.center = newCenter;
    [self.anomalyView setNeedsDisplay];
    if (gesture.state == UIGestureRecognizerStateEnded) {
        self.nearest = -1;
    }
    
    [self rightBarButtonAction:nil];
}




@end
